using Function.SerializedPropertyAttribute;
using SCore.Util;
using UnityEditor;
using UnityEditor.AddressableAssets.Build;
using UnityEditor.AddressableAssets.Settings;
using UnityEngine;

namespace Editor.Gmx.AutoGroup
{
    [CustomEditor(typeof(AutoAddressableTemplate))]
    public class AutoAddressableTemplateEditor : ObjectDrawEditor<AutoAddressableTemplate>
    {
    }

    public enum BuildOrLoadPath
    {
        Local = 1,
        Remote = 2,
    }

    [CreateAssetMenu(fileName = "AutoGroupSetting", menuName = "ScriptableObject/Framework/AutoAddressableTemplate",
        order = 1)]
    public class AutoAddressableTemplate : SingleScriptableObject<AutoGroupSetting>
    {
        public bool _buildRemoteCatalog;

        [DefinedValues(nameof(GetRemoteBuildPath))]
        public string _remoteCatalogBuildPath;

        [DefinedValues(nameof(GetRemoteLoadPath))]
        public string _remoteCatalogLoadPath;

        public int _maxConcurrentWebRequests;
        public int _catalogRequestsTimeout;
        public ShaderBundleNaming _shaderBundleNaming = ShaderBundleNaming.ProjectName;
        public MonoScriptBundleNaming _monoScriptBundleNaming = MonoScriptBundleNaming.Disabled;
        public AddressableAssetProfileSettings _addressableAsset;
        [DefinedValues(nameof(GetProfiles))] public string _profileName;

        public string[] GetRemoteBuildPath()
        {
            return new[]
            {
                AddressableAssetSettings.kLocalBuildPath,
                AddressableAssetSettings.kRemoteBuildPath
            };
        }

        public string[] GetRemoteLoadPath()
        {
            return new[]
            {
                AddressableAssetSettings.kLocalLoadPath,
                AddressableAssetSettings.kRemoteLoadPath
            };
        }

        public string[] GetProfiles()
        {
            return _addressableAsset.GetAllProfileNames().ToArray();
        }

        public void SyncSettings(AddressableAssetSettings settings)
        {
            bool isChange = false;
            if (settings.ShaderBundleNaming != _shaderBundleNaming)
            {
                settings.ShaderBundleNaming = _shaderBundleNaming;
                isChange = true;
            }

            if (settings.MonoScriptBundleNaming != _monoScriptBundleNaming)
            {
                settings.MonoScriptBundleNaming = _monoScriptBundleNaming;
                isChange = true;
            }

            if (settings.MaxConcurrentWebRequests != _maxConcurrentWebRequests)
            {
                settings.MaxConcurrentWebRequests = _maxConcurrentWebRequests;
                isChange = true;
            }


            if (settings.BuildRemoteCatalog != _buildRemoteCatalog)
            {
                settings.BuildRemoteCatalog = _buildRemoteCatalog;
                isChange = true;
            }

            if (settings.RemoteCatalogBuildPath.GetName(settings) != _remoteCatalogBuildPath)
            {
                settings.RemoteCatalogBuildPath.SetVariableByName(settings, _remoteCatalogBuildPath);
                isChange = true;
            }

            if (settings.RemoteCatalogLoadPath.GetName(settings) != _remoteCatalogLoadPath)
            {
                settings.RemoteCatalogLoadPath.SetVariableByName(settings, _remoteCatalogBuildPath);
                isChange = true;
            }

            if (settings.CatalogRequestsTimeout != _catalogRequestsTimeout)
            {
                settings.CatalogRequestsTimeout = _catalogRequestsTimeout;
                isChange = true;
            }

            foreach (var profileName in _addressableAsset.GetAllProfileNames())
            {
                var tempProfileId = _addressableAsset.GetProfileId(profileName);
                var profileId = settings.profileSettings.GetProfileId(profileName);
                if (profileId.IsNullOrEmpty())
                {
                    isChange = true;
                    profileId = settings.profileSettings.AddProfile(profileName, null);
                    var variableNames = _addressableAsset.GetVariableNames();
                    foreach (var variableName in variableNames)
                    {
                        settings.profileSettings.SetValue(profileId, variableName,
                            _addressableAsset.GetValueByName(tempProfileId, variableName));
                    }
                }
                else
                {
                    var variableNames = _addressableAsset.GetVariableNames();
                    foreach (var variableName in variableNames)
                    {
                        if (settings.profileSettings.GetValueByName(profileId, variableName) !=
                            _addressableAsset.GetValueById(tempProfileId, variableName))
                        {
                            isChange = true;
                            settings.profileSettings.SetValue(profileId, variableName,
                                _addressableAsset.GetValueByName(tempProfileId, variableName));
                        }
                    }
                }
            }

            if (settings.profileSettings.GetProfileName(settings.activeProfileId) != _profileName)
            {
                settings.activeProfileId = settings.profileSettings.GetProfileId(_profileName);
                isChange = true;
            }

            if (!settings.DisableCatalogUpdateOnStartup)
            {
                settings.DisableCatalogUpdateOnStartup = true;
                isChange = true;
            }

            if (isChange)
            {
                EditorUtility.SetDirty(settings);
                AssetDatabase.SaveAssetIfDirty(settings);
            }
        }
    }
}